package com.aduerr.v1shouyin.mvp.activity;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.mvp.adpter.MemberListAdpter;
import com.aduerr.v1shouyin.mvp.adpter.OrderinquireStickrecycleadpter;
import com.aduerr.v1shouyin.mvp.fragment.MemberRecordFregment;
import com.aduerr.v1shouyin.mvp.fragment.SaomaFregment;

import java.util.ArrayList;

/**
 * 订单查询页面
 */

public class OrderInquireActivity extends AppCompatActivity {


    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.tv_saomadingdan)
    TextView tvSaomadingdan;
    @BindView(R.id.tv_huiyuandingdan)
    TextView tvHuiyuandingdan;
    @BindView(R.id.vp)
    ViewPager vp;

    private OrderinquireStickrecycleadpter stickrecycleadpter;
    private int page = 1;
    private MemberListAdpter memberListAdpter;

    private int type = 0;//0 左边的 按钮   1右边的 按钮
    private ArrayList<Fragment> fragments;
    private OrderInquireActivity.myAdpter myAdpter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_inquire);
        ButterKnife.bind(this);
        initUI();
    }


    private void initUI() {
        tvTitle.setText("订单查询");
        tvRight.setText("筛选");
        tvSaomadingdan.setSelected(true);
        fragments = new ArrayList<>();
        fragments.add(new SaomaFregment());
        fragments.add(new MemberRecordFregment());
        myAdpter = new myAdpter(getSupportFragmentManager());
        vp.setAdapter(myAdpter);
        vp.setCurrentItem(0);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(position == 0) {
                    tvSaomadingdan.setSelected(true);
                    tvHuiyuandingdan.setSelected(false);
                } else {
                    tvSaomadingdan.setSelected(false);
                    tvHuiyuandingdan.setSelected(true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @OnClick({R.id.img_back, R.id.tv_right, R.id.tv_saomadingdan, R.id.tv_huiyuandingdan})
    public void onViewClicked(View view) {
        switch(view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_right:
                startActivity(new Intent(OrderInquireActivity.this,
                        OrderFiltrateActivity.class));
                break;
            case R.id.tv_saomadingdan://扫码订单
                tvSaomadingdan.setSelected(true);
                tvHuiyuandingdan.setSelected(false);
                vp.setCurrentItem(0);

                break;

            case R.id.tv_huiyuandingdan:////会员订单:

                tvSaomadingdan.setSelected(false);
                tvHuiyuandingdan.setSelected(true);
                vp.setCurrentItem(1);

                break;
        }
    }

//    private void initData(Context mContext, int page) {
//
//        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String currentDate = sDateFormat.format(new Date());
//        HashMap<String, String> addParams = new HashMap<>();
//        addParams.put("page", page + "");
//        addParams.put("beginDate", "1970-12-08 10:00:00");
//        addParams.put("endDate", currentDate);
////        addParams.put("state", "2");
////        addParams.putAll(serchmap);
////        getView().showProgress();
//        APIRetrofit.getAPIService().orderGetPosOrderL(RXRequest.getXingPosParams
//        (addParams, mContext))
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Observer<OrderGetPosOrderLEntity>() {
//
//
//                    @Override
//                    public void onSubscribe(@NonNull Disposable d) {
//
//                    }
//
//                    @Override
//                    public void onNext(@NonNull OrderGetPosOrderLEntity
//                    orderGetPosOrderLEntity) {
//                        int success = orderGetPosOrderLEntity.getSuccess();
//                        if (success == 1) {
//                            int PageCount = orderGetPosOrderLEntity.getPageCount();
//                            int PageIndex = orderGetPosOrderLEntity.getPageIndex();
//
//                            LogUtils.e("PageCount",
//                            "================================" + PageCount);
//                            LogUtils.e("pageIndex",
//                            "================================" + PageIndex);
//                            List<OrderGetPosOrderLEntity.DataBean> data =
//                            orderGetPosOrderLEntity.getData();
//                            if (data.size() > 0) {
//                                if (PageIndex > 1) {
//                                    LogUtils.e("loadmore",
//                                    "================================" + data.size());
//                                    stickrecycleadpter.loadmore(data);
//                                } else {
//                                    LogUtils.e("refresh",
//                                    "================================" + data.size());
//                                    stickrecycleadpter.refresh(data);
//                                }
//                            } else {
//                                ToastUtils.showToast(OrderInquireActivity.this,
//                                "没有数据了");
//                            }
//                        } else if (success == -100 | success == -200) {
//                            //// TODO: 2017/12/9 0009 绑定guid  界面
//                            startActivity(new Intent(OrderInquireActivity.this,
//                            BangGuidActivity.class));
//                            finish();
//                        } else if (success == -300) {
//                            //// TODO: 2017/12/9 0009 去登录的界面
//                            startActivity(new Intent(OrderInquireActivity.this,
//                            LoginActivity.class));
//                            finish();
//                        } else {
//                            ToastUtils.showToast(OrderInquireActivity.this,
//                            orderGetPosOrderLEntity.getErrMsg() + "");
//                        }
//
//                    }
//
//                    @Override
//                    public void onError(@NonNull Throwable e) {
//                        if (e instanceof SocketTimeoutException) {
//                            Toast.makeText(mContext, "网络连接超时", Toast.LENGTH_SHORT)
//                            .show();
//                        } else if (e instanceof ConnectException) {
//                            Toast.makeText(mContext, "连接失败", Toast.LENGTH_SHORT).show();
//                        } else {
//                            Log.e("onError:", "error:" + e.getMessage());//R.string
//                            .checknet +
//                            Toast.makeText(mContext, "检查网络状态", Toast.LENGTH_SHORT)
//                            .show();
//                        }
//                    }
//
//                    @Override
//                    public void onComplete() {
//
//                    }
//                });
//
//
//    }
//
//
//    private void initMemberData(Context mContext, int page) {
//
//        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String currentDate = sDateFormat.format(new Date());
//        HashMap<String, String> addParams = new HashMap<>();
//        addParams.put("page", page + "");
////        addParams.put("beginDate", "1970-12-08 10:00:00");
////        addParams.put("endDate", currentDate);
////        addParams.put("state", "2");
////        getView().showProgress();
//        APIRetrofit.getAPIService().GetMemberList(RXRequest.getXingPosParams
//        (addParams, mContext))
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new BaseObserver<GetMemberListEntity>(mContext) {
//                    @Override
//                    public void onResponse(@NonNull GetMemberListEntity
//                    getMemberListEntity) {
//                        int success = getMemberListEntity.getSuccess();
//                        if (success == 1) {
//                            int PageCount = getMemberListEntity.getPageCount();
//                            int PageIndex = getMemberListEntity.getPageIndex();
//                            LogUtils.e("测试", PageIndex +
//                            "================================" + PageCount);
//                            List<GetMemberListEntity.DataBean> data =
//                            getMemberListEntity.getData();
//                            if (data.size() > 0) {
//                                if (PageIndex > 1) {
//                                    LogUtils.e("loadmore",
//                                    "================================" + data.size());
//                                    memberListAdpter.loadmore(data);
//                                } else {
//                                    LogUtils.e("refresh",
//                                    "================================" + data.size());
//                                    memberListAdpter.refresh(data);
//                                }
//                            } else {
//                                ToastUtils.showToast(OrderInquireActivity.this,
//                                "没有数据了");
//                            }
//                        } else if (success == -100 | success == -200) {
//                            //// TODO: 2017/12/9 0009 绑定guid  界面
//                            startActivity(new Intent(OrderInquireActivity.this,
//                            BangGuidActivity.class));
//                            finish();
//                        } else if (success == -300) {
//                            //// TODO: 2017/12/9 0009 去登录的界面
//                            startActivity(new Intent(OrderInquireActivity.this,
//                            LoginActivity.class));
//                            finish();
//                        } else {
//                            ToastUtils.showToast(OrderInquireActivity.this,
//                            getMemberListEntity.getErrMsg() + "");
//                        }
//                    }
//                });
//    }

    class myAdpter extends FragmentPagerAdapter {

        public myAdpter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments != null ? fragments.size() : 0;
        }
    }


}
